【LeetCode 11】Container With Most Water 盛最多水的容器


“The Linux philosophy is “Laugh in the face of danger”.Oops.Wrong One. “Do it yourself”. Yes, that”s it.”
Linux的哲学就是“在危险面前放声大笑”,呵呵,不是这句,应该是“一切靠自己,自力更生”才对。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
*
* 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,
* 垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,
* 使得它们与 x 轴共同构成的容器可以容纳最多的水。
* 说明:你不能倾斜容器,且 n 的值至少为 2。
*
* 最大面积取决于短边,所以每次移动较短的一边即可
*/

public class leetcode11 {
public static int maxArea (int[] height) {
if (height.length < 2)
return 0;
int left = 0;
int right = height.length-1;
int max = 0;
while (left < right) {
int h = Math.min(height[left], height[right]);
max = Math.max(max, h * (right - left));
if (h == height[left])
left++;
else
right--;
}
return max;
}
public static void main(String []args){
int []h = {1,8,6,2,5,4,8,3,7};
leetcode11.maxArea(h);
}
}
Thanks!